#include "img_widget.h"

img_Widget::img_Widget(vtkRenderWindow *_renderwindow, quint8 _type, QObject *parent)
    : QObject(parent)
    , m_window(_renderwindow)
    , m_render(vtkSmartPointer<vtkRenderer>::New())
    , m_ipw(vtkSmartPointer<vtkImagePlaneWidget>::New())
    , m_viewer(vtkSmartPointer<vtkResliceImageViewer>::New())
    , m_analysis(vtkSmartPointer<vtkResliceImageViewer>::New())
    , m_crossLineStyle(vtkSmartPointer<vtkCrossLineStyle>::New())
    , m_colorTable(vtkSmartPointer<vtkLookupTable>::New())
    , m_crossLine(new vtk_CrossLine(_type))
    , m_type(_type)
{
    m_viewer->SetRenderWindow(m_window);
    m_viewer->SetupInteractor(m_window->GetInteractor());
    //------------------------------------------------------
    m_crossLineStyle->widget = this;
    m_defaultStyle = m_window->GetInteractor()->GetInteractorStyle();
    //初始化色彩映射表
    m_colorTable->SetNumberOfColors(2);
    m_colorTable->SetTableRange(0, 1);
    m_colorTable->SetTableValue(0, 0.0, 0.0, 0.0, 0.0);
    m_colorTable->SetTableValue(1, 1.0, 0.0, 0.0, 0.1);
    m_colorTable->Build();
}

img_Widget::~img_Widget()
{
}

void img_Widget::setImg(img_File * img)
{
    m_img = img;
    unSetAnalysisImg();
    //---------------------------------------------------------------------
    //初始化十字线
    m_crossLine->setSize(&img->info());
    //---------------------------------------------------------------------
    //初始化显示图像
    m_viewer->SetInputData(img->image());
    m_viewer->SetSliceOrientation(m_type);
    m_viewer->SetColorWindow(img->getWindow());
    m_viewer->SetColorLevel(img->getLevel());
    m_viewer->SetSlice(img->info().dim[m_type + 1] / 2);
    m_viewer->GetRenderer()->SetBackground(0.1, 0.1, 0.1);
    m_window->Render();
    //---------------------------------------------------------------------
    //初始化3D视图切片
    m_ipw->SetInputData(img->image());
    m_ipw->SetInputData(img->image());
    m_ipw->SetPlaneOrientation(m_type);
    m_ipw->SetWindowLevel(img->getWindow(), img->getLevel());
    m_ipw->SetSliceIndex(img->info().dim[m_type + 1] / 2);
    m_ipw->On();
    m_ipw->InteractionOn();
    emit sliceChange(m_viewer->GetSlice());
}

void img_Widget::setAnalysisImg(vtkImageData *img)
{
    //配置颜色映射表
    m_analysis->SetInputData(img);
    m_analysis->SetRenderWindow(m_viewer->GetRenderWindow());
    m_analysis->SetSliceOrientation(m_type);
    m_analysis->SetSlice(m_viewer->GetSlice());
    m_analysis->GetImageActor()->SetInterpolate(false);
    m_analysis->GetImageActor()->GetProperty()->SetLookupTable(m_colorTable);
    m_analysis->GetImageActor()->GetProperty()->SetDiffuse(0.0);
    m_analysis->GetImageActor()->SetPickable(false);
    m_analysis->GetRenderer()->SetBackground(0.1, 0.1, 0.1);
    m_analysis->GetRenderer()->AddActor(m_viewer->GetImageActor());
    m_analysis->Render();
    anlsExist = true;
}

void img_Widget::unSetAnalysisImg()
{
    m_analysis->GetRenderer()->RemoveActor(m_viewer->GetImageActor());
    m_window->RemoveRenderer(m_analysis->GetRenderer());
    m_window->Render();
    m_analysis = vtkSmartPointer<vtkResliceImageViewer>::New();
    anlsExist = false;
}

void img_Widget::setCenter(double _center[], const quint8 type)
{
    double center[3];
    m_crossLine->getCenter(center);
    for(int i = 0; i < 3; i++) {//掩掉其余坐标，仅剩余对坐标发生改变的轴
        if(i != type && i != m_type) {
            center[i] = _center[i];
            break;
        }
    }
    m_crossLine->setCenter(center);
    int slice = _center[m_type] / m_img->info().pixdim[m_type + 1];
    m_viewer->SetSlice(slice);//设置slice
    m_analysis->SetSlice(slice);
    m_ipw->SetSliceIndex(slice);
    emit sliceChange(slice);
}

void img_Widget::setWindowLevel(double _window, double _level)
{
    m_viewer->SetColorWindow(_window);
    m_viewer->SetColorLevel(_level);
    m_viewer->Render();
    m_ipw->SetWindowLevel(_window, _level);
}

void img_Widget::setCrossLineMode(bool mode)
{
    if(mode) {
        m_viewer->GetRenderer()->AddActor(m_crossLine->getActor());
        //resliceimageviewer继承了imageviewer2的setslice，此处只能用边际效用得到显示刷新
        m_viewer->SetSlice(m_viewer->GetSlice() + 1);
        m_viewer->SetSlice(m_viewer->GetSlice() - 1);
        m_window->GetInteractor()->SetInteractorStyle(m_crossLineStyle);
    } else {
        m_viewer->GetRenderer()->RemoveActor(m_crossLine->getActor());
        m_window->Render();
        m_window->GetInteractor()->SetInteractorStyle(m_defaultStyle);
    }
}

void img_Widget::setLabelOpacity(int _value)
{
    m_colorTable->SetTableValue(1, 1.0, 0.0, 0.0, _value / 100.0);
    m_colorTable->Build();
    m_analysis->Render();
}

bool img_Widget::isOpen()
{
    return m_img != nullptr;
}

bool img_Widget::isAnlsExist()
{
    return anlsExist;
}

quint8 img_Widget::getType()
{
    return m_type;
}

const img_Info * img_Widget::getImgInfo()
{
    return &m_img->info();
}

vtk_CrossLine * img_Widget::getCrossLine()
{
    return m_crossLine;
}

void img_Widget::getWindowLevel(double wl[2])
{
    wl[0] = m_viewer->GetWindowLevel()->GetWindow();
    wl[1] = m_viewer->GetWindowLevel()->GetLevel();
}

vtkCrossLineStyle *img_Widget::getCrossLineStyle()
{
    return m_crossLineStyle;
}

vtkInteractorObserver *img_Widget::getDefaultStyle()
{
    return m_defaultStyle;
}

vtkRenderWindow *img_Widget::getRenderWindow()
{
    return m_window;
}

vtkResliceImageViewer *img_Widget::getAnalysisViewer()
{
    return m_analysis;
}

vtkResliceImageViewer *img_Widget::getImageViewer()
{
    return m_viewer;
}

vtkImagePlaneWidget *img_Widget::getImagePlaneWidget()
{
    return m_ipw;
}
